package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.Tools;
import com.monkeygroup.mygoplus.common.utils.image.Images;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.http.ThingsHttpModel;
import com.monkeygroup.mygoplus.thing.vo.MyImageVO;
import com.monkeygroup.mygoplus.thing.vo.RentVO;
import com.monkeygroup.mygoplus.thing.vo.UserVO;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.home.activity.rent.PublishedRentActivity;
import com.monkeygroup.mygoplus.view.home.activity.rent.RentHouseDetailActivity;

import java.io.Serializable;
import java.util.List;

/**
 * 物件管理
 * Created by LCW on 2017/6/6.
 */

public class PersonalPropertyManageActivity extends BaseActivity implements View.OnClickListener{

    private TextView txtTile,txtPersonalPropertyRent,txtPersonalPropertySale,txtPersonalPropertyAssign,txtPersonalPropertyNewCase;
    private Button btnPersonalMyProperty,btnPersonalSeekProperty;
    ArrayAdapter adapter;
    private SwipeMenuListView mListView;
    //請求
    private ThingsHttpModel httpThingsModel;
    List<RentVO> thingsModel;
    private PersonalNomalInfo user;

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonalPropertyManageActivity.class);

        activity.startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.txt_personal_property_rent:
                txtPersonalPropertyRent.setTextColor(this.getResources().getColor(R.color.font_things_red));
                txtPersonalPropertySale.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyAssign.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyNewCase.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                adapter = new MyRentAdapter(this, R.layout.personal_property_manage_rent_list_item);
                if(btnPersonalMyProperty.getCurrentTextColor()==Color.WHITE){
                    rentCreator(1);
                }else{
                    seekCreator();
                }
                user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
                String param ="?user="+6829+"&type=rent";
                initRequest(param);
                mListView.setAdapter(adapter);
                break;
            case R.id.txt_personal_property_sale:
                txtPersonalPropertyRent.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertySale.setTextColor(this.getResources().getColor(R.color.font_things_red));
                txtPersonalPropertyAssign.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyNewCase.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                adapter = new MyRentAdapter(this, R.layout.personal_property_manage_rent_list_item);
                if(btnPersonalMyProperty.getCurrentTextColor()==Color.WHITE){
                    rentCreator(2);
                }else{
                    seekCreator();
                }
                user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
                param = "?user=" + 6829 + "&type=buy";
                initRequest(param);
                mListView.setAdapter(adapter);
                break;
            case R.id.txt_personal_property_assign:
                txtPersonalPropertyRent.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertySale.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyAssign.setTextColor(this.getResources().getColor(R.color.font_things_red));
                txtPersonalPropertyNewCase.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                adapter = new MyRentAdapter(this, R.layout.personal_property_manage_rent_list_item);
                if(btnPersonalMyProperty.getCurrentTextColor()==Color.WHITE){
                    rentCreator(3);
                }else{
                    seekCreator();
                }
                user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
                param = "?user=" + 6829 + "&type=buy";
                initRequest(param);
                mListView.setAdapter(adapter);
                break;
            case R.id.txt_personal_property_newcase:
                txtPersonalPropertyRent.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertySale.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyAssign.setTextColor(this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyNewCase.setTextColor(this.getResources().getColor(R.color.font_things_red));
                adapter = new MyRentAdapter(this, R.layout.personal_property_manage_rent_list_item);
                if(btnPersonalMyProperty.getCurrentTextColor()==Color.WHITE){
                    rentCreator(4);
                }else{
                    seekCreator();
                }
                user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
                param = "?user=" + 6829 + "&type=all";
                initRequest(param);
                mListView.setAdapter(adapter);
                break;
            default:
                break;
        }
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.personal_property_manage);
    }

    @Override
    public void initView() {
        txtTile = (TextView)findViewById(R.id.txt_title);
        txtPersonalPropertyRent = (TextView) findViewById(R.id.txt_personal_property_rent);
        txtPersonalPropertySale = (TextView) findViewById(R.id.txt_personal_property_sale);
        txtPersonalPropertyAssign = (TextView) findViewById(R.id.txt_personal_property_assign);
        txtPersonalPropertyNewCase = (TextView) findViewById(R.id.txt_personal_property_newcase);
        btnPersonalMyProperty = (Button) findViewById(R.id.btn_personal_my_property);
        btnPersonalSeekProperty = (Button) findViewById(R.id.btn_personal_my_seek_property);
        txtTile.setText("物件管理");
        adapter = new MyRentAdapter(this, R.layout.personal_property_manage_rent_list_item);
        rentCreator(1);
        user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
        String param ="?user="+6829+"&type=rent";
        initRequest(param);
        mListView.setAdapter(adapter);
    }

    private class MyRentAdapter extends ArrayAdapter<RentVO> {

        private int resourceId;
        private Context context;

        public MyRentAdapter(Context context, int resourceId){
            super(context, resourceId);
            this.context = context;
            this.resourceId = resourceId;
        }

        @Override
        public long getItemId(int position) {
            RentVO vo = (RentVO) mListView.getItemAtPosition(position);
            return super.getItemId(position);
        }
        @Override
        public int getCount() {
            return super.getCount();
        }
        @Override
        public RentVO getItem(int position) {
            return super.getItem(position);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final RentVO vo = getItem(position);
            View view = null;
            final ItemView itemView;
            if(convertView == null){
                itemView = new ItemView();
                view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                itemView.houseImg = (ImageView)view.findViewById(R.id.img_sell_house_item);
                itemView.areaName = (TextView)view.findViewById(R.id.txt_sell_house_item_area);
                itemView.houseName = (TextView)view.findViewById(R.id.txt_sell_house_item_name);
                itemView.housePrice = (TextView)view.findViewById(R.id.txt_sell_house_item_price);
                view.setTag(itemView);
            } else {
                view = convertView;
                itemView = (ItemView)view.getTag();
            }

            Images.with(context).load(Uri.parse(vo.getImg())).resize(130,90).into(itemView.houseImg);
            itemView.houseName.setText(vo.getName());
            if(vo.getRentprice()!=0){
                itemView.housePrice.setText(vo.getRentprice()+"元");
            }
            if(vo.getSaleprice()!=0){

                itemView.housePrice.setText(vo.getSaleprice()+"萬");
            }
            if(vo.getUnitprice()!=0){
                itemView.housePrice.setText(vo.getUnitprice()+"萬/每平米");
                itemView.areaName.setVisibility(View.GONE);
            }
            if(vo.getUnitprice1()!=0&&vo.getUnitprice2()!=0){
                itemView.housePrice.setText(vo.getUnitprice1()+"~"+vo.getUnitprice1()+"萬");
            }
            if(vo.getLawnarea()!=null){
                itemView.areaName.setText(vo.getLawnarea()+"坪");
            }
            else if("1".equals(vo.getType())){

                itemView.areaName.setText("预售屋");
            }
            else if("3".equals(vo.getType())){

                itemView.areaName.setText("新成屋");
            }
            else if("4".equals(vo.getType())){

                itemView.areaName.setText("建构中");
            }

            return view;
        }
        class ItemView {
            ImageView houseImg;
            TextView houseName;
            TextView housePrice;
            TextView areaName;
        }
        /**
         * 局部刷新
         * @param position
         */
        public void updateView(int position) {
            View view = mListView.getChildAt(position);
            LinearLayout layout = (LinearLayout) findViewById(R.id.personal_property_item);
            layout.setVisibility(View.VISIBLE);
        }
    }

    private void initRequest(String param){
        httpThingsModel = new ThingsHttpModel(RequestUrl.getPersonalPropertyUrl + param ,
                new ThingsHttpModel.SimpleOnHttpRequestListener() {
                    @Override
                    public void onRequestFinished(String resultCode, Integer timestamp) {
                    }
                    @Override
                    public void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img) {
                        Gson gson = new Gson();
                        thingsModel = gson.fromJson(gson.toJson(property), new TypeToken<List<RentVO>>() {}.getType());
                        for (RentVO rentVO:thingsModel){
                            adapter.add(rentVO);
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img) {

                    }
                },true);
        httpThingsModel.requestThings();
    }

    public void initDetailRequest(String param) {
        httpThingsModel = new ThingsHttpModel(RequestUrl.getPropertyUrl + param,
                new ThingsHttpModel.SimpleOnHttpRequestListener() {
                    @Override
                    public void onRequestFinished(String resultCode, Integer timestamp) {

                    }

                    @Override
                    public void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img) {
                        Gson gson = new Gson();
                        List<MyImageVO> imgModel = gson.fromJson(gson.toJson(img), new TypeToken<List<MyImageVO>>() {
                        }.getType());
                        RentVO propertyModel = gson.fromJson(gson.toJson(property), new TypeToken<RentVO>() {
                        }.getType());
                        UserVO userVO = gson.fromJson(gson.toJson(user), new TypeToken<UserVO>() {
                        }.getType());

                        Bundle bundle = new Bundle();
                        bundle.putSerializable("imgModel", (Serializable) imgModel);
                        bundle.putSerializable("propertyModel", propertyModel);
                        bundle.putSerializable("userVO", userVO);
                        Intent intent = null;
                        intent = new Intent(PersonalPropertyManageActivity.this,RentHouseDetailActivity.class);
                        intent.putExtra("param", bundle);
                        PersonalPropertyManageActivity.this.startActivityForResult(intent, 3);
                    }

                    @Override
                    public void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img) {

                    }
                }, true);
        httpThingsModel.requestThings();
    }

    private void rentCreator(final int type){
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                //编辑
                SwipeMenuItem edit = new SwipeMenuItem(getApplicationContext());
                edit.setBackground(new ColorDrawable(Color.parseColor("#BCBCC4")));
                edit.setWidth(Tools.dip2px(getApplicationContext(),90));
                edit.setTitle("編輯");
                edit.setTitleSize(18);
                edit.setTitleColor(Color.WHITE);
                if(type!=3&&type!=4){
                    menu.addMenuItem(edit);
                }
                //讯息
                SwipeMenuItem message = new SwipeMenuItem(getApplicationContext());
                message.setBackground(new ColorDrawable(Color.parseColor("#AAAAB4")));
                message.setWidth(Tools.dip2px(getApplicationContext(),90));
                message.setTitle("訊息");
                message.setTitleSize(18);
                message.setTitleColor(Color.WHITE);
                menu.addMenuItem(message);
                //交易
                SwipeMenuItem deal = new SwipeMenuItem(getApplicationContext());
                deal.setBackground(new ColorDrawable(Color.parseColor("#FCB749")));
                deal.setWidth(Tools.dip2px(getApplicationContext(),90));
                deal.setTitle("交易");
                deal.setTitleSize(18);
                deal.setTitleColor(Color.WHITE);
                if(type!=4){
                    menu.addMenuItem(deal);
                }
                //下架
                SwipeMenuItem offItem = new SwipeMenuItem(getApplicationContext());
                offItem.setBackground(new ColorDrawable(Color.parseColor("#FE3B2F")));
                offItem.setWidth(Tools.dip2px(getApplicationContext(),90));
                offItem.setTitle("下架");
                offItem.setTitleSize(18);
                offItem.setTitleColor(Color.WHITE);
                if(type!=4){
                    menu.addMenuItem(offItem);
                }
            }
        };
        mListView = (SwipeMenuListView) findViewById(R.id.listView_manage_rent);
        mListView.setMenuCreator(creator);
        mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String param ="?seq="+thingsModel.get(i).getSeq();
                initDetailRequest(param);
            }
        });
        // 设置点击不同菜单时触法的事件
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                // index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值，类似数组的下标。
                // 从0开始，依次是：0、1、2、3...
                switch (index) {
                    case 0:
                        if(type==1||type==2){
                            PublishedRentActivity.gotoActivity(PersonalPropertyManageActivity.this,null,"1");
                        }
                        break;
                    case 1:

                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
    }

    private void seekCreator(){
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                //讯息
                SwipeMenuItem message = new SwipeMenuItem(getApplicationContext());
                message.setBackground(new ColorDrawable(Color.parseColor("#AAAAB4")));
                message.setWidth(Tools.dip2px(getApplicationContext(),90));
                message.setTitle("訊息");
                message.setTitleSize(18);
                message.setTitleColor(Color.WHITE);
                menu.addMenuItem(message);
                //交易
                SwipeMenuItem deal = new SwipeMenuItem(getApplicationContext());
                deal.setBackground(new ColorDrawable(Color.parseColor("#FCB749")));
                deal.setWidth(Tools.dip2px(getApplicationContext(),90));
                deal.setTitle("交易");
                deal.setTitleSize(18);
                deal.setTitleColor(Color.WHITE);
                menu.addMenuItem(deal);
            }
        };
        mListView = (SwipeMenuListView) findViewById(R.id.listView_manage_rent);
        mListView.setMenuCreator(creator);
        mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String param ="?seq="+thingsModel.get(i).getSeq();
                initDetailRequest(param);
            }
        });
        // 设置点击不同菜单时触法的事件
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                // index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值，类似数组的下标。
                // 从0开始，依次是：0、1、2、3...
                switch (index) {
                    case 0:

                        break;
                    case 1:

                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
    }

    @Override
    public void setListener() {
        findViewById(R.id.txt_personal_property_rent).setOnClickListener(this);
        findViewById(R.id.txt_personal_property_sale).setOnClickListener(this);
        findViewById(R.id.txt_personal_property_assign).setOnClickListener(this);
        findViewById(R.id.txt_personal_property_newcase).setOnClickListener(this);
    }

    @Override
    public void doEvent() {
        btnPersonalMyProperty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnPersonalMyProperty.setBackgroundResource(R.drawable.btn_personal_left_pressed);
                btnPersonalMyProperty.setTextColor(Color.WHITE);
                btnPersonalSeekProperty.setBackgroundResource(R.drawable.btn_personal_right_normal);
                btnPersonalSeekProperty.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.tab_red_color));
                txtPersonalPropertyRent.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.font_things_red));
                txtPersonalPropertySale.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyAssign.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyNewCase.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.bottom_gray));
                adapter = new MyRentAdapter(PersonalPropertyManageActivity.this, R.layout.personal_property_manage_rent_list_item);
                rentCreator(1);
                user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
                String param ="?user="+6829+"&type=rent";
                initRequest(param);
                mListView.setAdapter(adapter);
            }
        });
        btnPersonalSeekProperty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnPersonalSeekProperty.setBackgroundResource(R.drawable.btn_personal_right_pressed);
                btnPersonalSeekProperty.setTextColor(Color.WHITE);
                btnPersonalMyProperty.setBackgroundResource(R.drawable.btn_personal_left_normal);
                btnPersonalMyProperty.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.tab_red_color));
                txtPersonalPropertyRent.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.font_things_red));
                txtPersonalPropertySale.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyAssign.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.bottom_gray));
                txtPersonalPropertyNewCase.setTextColor(PersonalPropertyManageActivity.this.getResources().getColor(R.color.bottom_gray));
                adapter = new MyRentAdapter(PersonalPropertyManageActivity.this, R.layout.personal_property_manage_rent_list_item);
                seekCreator();
                user= LocalInfo.getUserInfo(PersonalPropertyManageActivity.this);
                String param ="?user="+6829+"&type=rent";
                initRequest(param);
                mListView.setAdapter(adapter);
            }
        });
    }
}
